package com.yanu.t2s.controller;

import com.yanu.t2s.entity.User;
import com.yanu.t2s.entity.Yangzhi;
import com.yanu.t2s.service.TuzaiService;
import com.yanu.t2s.service.UserService;
//<<<<<<< HEAD
import com.yanu.t2s.service.YangzhiService;
//=======
import net.minidev.json.JSONObject;
//>>>>>>> 711cb066a277f8b98d72c3c973a587e2b804358c
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.support.NullValue;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
//<<<<<<< HEAD
//@RequestMapping("login")
//=======
@RequestMapping("/")
//>>>>>>> 711cb066a277f8b98d72c3c973a587e2b804358c
public class RegisterController { //试试能不能提交 zdx
    @Autowired
    UserService userService;

    @Autowired
    TuzaiService tuzaiService;

    //注册用户
    @Transactional()
    @RequestMapping(value = "register",method={RequestMethod.POST, RequestMethod.GET},produces = "application/json;charset=UTF-8")
    String Login(/*@RequestParam String name,@RequestParam String password ,@RequestParam String usertype*/ User user) {
//        JSONObject result = new JSONObject();
//        User user=userService.getUserByName(name);
//        System.out.println(user.toString());
//        result.put("name", user.getName());
//        result.put("password", user.getPassword());
//        result.put("uesrtype", user.getUsertype());
//        return result.toJSONString();
          //User user=new User();
          //user.setName(name);
          //user.setPassword(password);
          //user.setUsertype(usertype);
          System.out.println("cheng gong le "+user);
          System.out.println("cheng gong le "+user.getClass());
          System.out.println("cheng gong le "+user.getAccount());
          User oldUser =userService.getUserByName(user.getAccount());
          System.out.print("olduser:");
          System.out.println(oldUser);
          if(oldUser==null){
              if(!user.getUsertype().equals("tuzai")) {
                  userService.addUser(user);
              }
              else{
                  userService.addUser(user);
                  User nowuser=userService.getUserByName(user.getAccount());
                  tuzaiService.addTuzai(nowuser.getId(),nowuser.getAccount());
              }
              return "tt";
          }
          else{
              return "xx";
          }
        //System.out.println("cheng gong le"+user);
        //return "";
    }

//    @RequestMapping(value = "testregister",method={RequestMethod.POST, RequestMethod.GET},produces = "application/json;charset=UTF-8")
//    String test(@RequestParam int id,@RequestParam String account) {
//
//       // userService.addUser(user);
//        tuzaiService.addTuzai(id,account);
//        return  "chenggong"  ;
//
//    }



}


